数组
数组是值的有序集合。每个值叫做元素,每个元素在数组中都有数字位置编号,也就是索引。JS中的数组是弱类型的,数组中可以含有不同类型的元素。数组元素甚至可以是对象或其它数组。
var arr = [1, true, null, undefined, {x : 1}, [1, 2, 3]];
|
创建数组
创建数组-字面量
var BAT = ['Alibaba', 'Tencent', 'Baidu']; var students = [{name : 'Bosn', age : 27}, {name : 'Nunnly', age : 3}]; var arr = ['Nunnly', 'is', 'big', 'keng', 'B', 123, true, null]; var arrInArr = [[1, 2], [3, 4, 5]]; var commasArr1 = [1, , 2]; var commasArr2 = [,,];
|
创建数组-new Array
var arr = new Array(); var arrWithLength = new Array(100); var arrLikesLiteral = new Array(true, false, null, 1, 2, "hi");
|
数组元素读写
var arr = [1, 2, 3, 4, 5]; arr[1]; arr.length; arr[5] = 6; arr.length; delete arr[0]; arr[0];
|
数组的增删
动态的,无需指定大小
var arr = []; arr[0] = 1; arr[1] = 2; arr.push(3); arr; arr[arr.length] = 4; arr; arr.unshift(0); arr; delete arr[2]; arr; arr.length; 2 in arr; arr.length -= 1; arr; arr.pop(); arr; arr.shift(); arr;
|
数组迭代
var i = 0, n = 10; var arr = [1, 2, 3, 4, 5]; for (; i < n; i++) { console.log(arr[i]); } for(i in arr) { console.log(arr[i]); } Array.prototype.x = 'inherited'; for(i in arr) { console.log(arr[i]); } for(i in arr) { if (arr.hasOwnProperty(i)) { console.log(arr[i]); } }
|
二维数组
var arr = [[0, 1], [2, 3], [4, 5]]; var i = 0, j = 0; var row; for (; i < arr.length; i++) { row = arr[i]; console.log('row ' + i); for (j = 0; j < row.length; j++) { console.log(row[j]); } }
|
稀疏数组
稀疏数组并不含有从0开始的连续索引。一般length属性值比实际元素个数大。
var arr1 = [undefined]; var arr2 = new Array(1); 0 in arr1; 0 in arr2; arr1.length = 100; arr1[99] = 123; 99 in arr1; 98 in arr1; var arr = [,,]; 0 in arr;
|
数组方法
{} => Object.prototype [] => Array.prototype
|
Array.prototype.join()
将数组转化成字符串
var arr = [1, 2, 3]; arr.join(); arr.join("_"); function repeatString(str, n) { return new Array(n + 1).join(str); } repeatString("a", 3); repeatString("Hi", 5);
|
Array.prototype.reverse
Array.prototype.sort
Notes:原数组被修改
var arr = ["a", "d", "c", "b"]; arr.sort(); arr = [13, 24, 51, 3]; arr.sort(); arr; arr.sort(function(a, b) { return a - b; }); arr = [{age : 25}, {age : 39}, {age : 99}]; arr.sort(function(a, b) { return a.age - b.age; }); arr.forEach(function(item) { console.log('age', item.age); });
|
Array.prototype.concat
Notes:原数组不被修改
var arr = [1, 2, 3]; arr.concat(4, 5); arr; arr.concat([10, 11], 13); arr.concat([1, [2, 3]]);
|
Array.prototype.slice
Notes:原数组未被修改
var arr = [1, 2, 3, 4, 5]; arr.slice(1, 3); arr.slice(1); arr.slice(1, -1); arr.slice(-4, -3);
|
Notes:原数组被修改
var arr = [1, 2, 3, 4, 5]; arr.splice(2); arr; arr = [1, 2, 3, 4, 5]; arr.splice(2, 2); arr; arr = [1, 2, 3, 4, 5]; arr.splice(1, 1, 'a', 'b'); arr;
|
Array.prototype.forEach
var arr = [1, 2, 3, 4, 5]; arr.forEach(function(x, index, a){ console.log(x + '|' + index + '|' + (a === arr)); });
|
Array.prototype.map
var arr = [1, 2, 3]; arr.map(function(x) { return x + 10; }); arr;
|
Array.prototype.filter
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; arr.filter(function(x, index) { return index % 3 === 0 || x >= 8; }); arr;
|
Array.prototype.indexOf&lastIndexOf
var arr = [1, 2, 3, 2, 1]; arr.indexOf(2); arr.indexOf(99); arr.indexOf(1, 1); arr.indexOf(1, -3); arr.indexOf(2, -1); arr.lastIndexOf(2); arr.lastIndexOf(2, -2); arr.lastIndexOf(2, -3);
|
Array.isArray
Array.isArray([]); [] instanceof Array; ({}).toString.apply([]) === '[object Array]'; [].constructor === Array;
|
字符串和数组
var str = "hello world"; str.charAt(0); str[1]; Array.prototype.join.call(str, "_"); var str = "hello world"; str.charAt(0); str[1]; Array.prototype.join.call(str, "_");
|